package com.scnlf.scnlf.portal.core.business.event.manager.impl;

import com.scnlf.scnlf.portal.core.business.common.enums.EventTypeEnum;
import com.scnlf.scnlf.portal.core.business.common.enums.SideEnum;
import com.scnlf.scnlf.portal.core.business.event.param.trade.CreateTradeEventParamParam;
import com.scnlf.scnlf.portal.core.business.event.param.trade.TradeExecReportParamParam;
import com.scnlf.scnlf.portal.core.business.loan.service.ILoanPositionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
public class TradeEventShortClosingServiceImpl extends AbstractTradeEventService {

  private final ILoanPositionService positionService;

  @Override
  public EventTypeEnum getEventType() {
    return EventTypeEnum.TRADE_SHORT;
  }

  @Override
  public SideEnum getSide() {
    return SideEnum.BUY;
  }

  @Override
  public void enableTrade(CreateTradeEventParamParam tradeEventParam) {
  }

  @Override
  public void beforeHandleTrade(CreateTradeEventParamParam createTradeEventParam) {
//    super.beforeHandleTrade(createTradeEventParam);
//    EventEntity eventEntity = createTradeEventParam.getEvent();
//    Long orderQty = eventEntity.getQuantity();
//    // 扣除当日可买数量、空头存续数量
//    positionService.updateQty(new PositionUpdateQtyParam()
//        .setPositionId(eventEntity.getPositionId())
//        .setAvailableBuyQty(orderQty)
//        .setLeaveShortQty(orderQty)
//    );
//
//    // 自动确认审批状态
//    TradeOrderEntity orderEntity = createTradeEventParam.getOrder();
//    orderEntity.setFlowStatus(FlowTypeEnum.CONFIRMED);
//    orderService.save(Collections.singletonList(orderEntity));
  }

  @Override
  public void handlingTradeExecReport(TradeExecReportParamParam tradeExecReportParam) {
//    EventEntity event = tradeExecReportParam.getEvent();
//    TradeExecReportDTO execReport = tradeExecReportParam.getExecReport();
//    Long quantity = execReport.getQuantity();
//
//    // 持仓更新
//    if(quantity > 0){
//      // 买完后加可还、可买、当日买入数量
//      positionService.updateQty(new PositionUpdateQtyParam()
//          .setPositionId(event.getPositionId())
//          .setIntradayBuyQty(-quantity)
//          .setReturnableQty(-quantity)
//      );
//    }
//
//    // 事件更新
//    event.setInitPrice(execReport.getExecPrice())
//        .setTargetStatus(TargetStatusEnum.SUCCESS);
//
//    eventService.save(Collections.singletonList(event));
  }
}
